package com.site.wisdom_construction.controller.publicModule;

import com.alibaba.fastjson.JSONObject;
import com.site.wisdom_construction.entity.publicModule.Position;
import com.site.wisdom_construction.responseEntity.JsonResult;
import com.site.wisdom_construction.responseEntity.OneDataResult;
import com.site.wisdom_construction.responseEntity.Page;
import com.site.wisdom_construction.service.publicModule.PositionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/** @Author daiyifei @Date 2022/1/4 @Description */
@Api(tags = "岗位信息接口")
@RestController
@RequestMapping("Position")
@CrossOrigin
public class PositionController {
  @Autowired private PositionService positionService;

  @ApiOperation(value = "增加岗位信息", notes = "测试数据:")
  @PostMapping("/insertPosition")
  public JsonResult insertPosition(@RequestBody Map map) {
    JsonResult jsonResult = new JsonResult();
    List<Position> positionList = positionService.findPositionByName(map);
    if (positionList.size() > 0) {
      jsonResult.setCode(200);
      jsonResult.setMessage("岗位信息已存在!");
    } else {
      int i = positionService.insertPosition(map);
      if (i == 1) {
        jsonResult.setCode(200);
        jsonResult.setMessage("成功录入岗位信息!");
      } else {
        jsonResult.setCode(20006);
        jsonResult.setMessage("录入岗位信息失败!");
      }
    }
    return jsonResult;
  }

  @ApiOperation(value = "删除岗位信息", notes = "测试数据:")
  @PostMapping("/deletePosition")
  public JsonResult deletePosition(@RequestBody Map map) {
    JsonResult jsonResult = new JsonResult();
    int i = positionService.deletePosition(map);
    if (i == 1) {
      jsonResult.setCode(200);
      jsonResult.setMessage("成功删除岗位信息!");
    } else {
      jsonResult.setCode(20006);
      jsonResult.setMessage("删除岗位信息失败!");
    }
    return jsonResult;
  }

  @ApiOperation(value = "修改岗位信息", notes = "测试数据:")
  @PostMapping("/updatePosition")
  public JsonResult updatePosition(@RequestBody Map map) {
    JsonResult jsonResult = new JsonResult();
    int i = positionService.updatePosition(map);
    if (i == 1) {
      jsonResult.setCode(200);
      jsonResult.setMessage("成功修改岗位信息!");
    } else {
      jsonResult.setCode(20006);
      jsonResult.setMessage("修改岗位信息失败!");
    }
    return jsonResult;
  }

  @ApiOperation(value = "分页模糊查询岗位信息", notes = "测试数据:")
  @PostMapping("/findPositionByPage")
  public Page findPositionByPage(@RequestBody Map map) {
    Page page = new Page();
    page.setPageNo((Integer) map.get("pageNo"));
    page.setPageSize((Integer) map.get("pageSize"));
    page.setTotal(positionService.Total(map));
    page.setItems(positionService.findPositionByPage(map));
    return page;
  }

  @ApiOperation(value = "下拉框查询岗位信息", notes = "测试数据:")
  @PostMapping("/selectPosition")
  public OneDataResult selectPosition(@RequestBody Map map) {
    OneDataResult oneDataResult = new OneDataResult();
    oneDataResult.setData(positionService.selectPosition(map));
    return oneDataResult;
  }

  @ApiOperation(value = "批量导入岗位信息", notes = "测试数据:")
  @PostMapping("/batchInsertPosition")
  public void batchInsertPosition(@RequestBody Map map) {
    List list = (List) map.get("data");
    for (int i = 0; i < list.size(); i++) {
      Map mapR = new HashMap();
      JSONObject json = (JSONObject) JSONObject.toJSON(list.get(i));
      mapR.put("professionName", json.get("name"));
      positionService.insertPosition(mapR);
    }
  }
}
